$(document).ready(function () {
	var addOrEdit = 0; //0代表添加，1代表编辑
	var deleteId = "";
	var addressId = "";
    var province = "";
    var city = "";
    var district = "";
    var address = "";
    
    $("#myOrderList").click(function () {
        location.href = 'myOrderList.html';
    })
    $("#personalInfo").click(function () {
        location.href = 'personalInfo.html';
    })
    $("#recAddress").click(function () {
        location.href = 'recAddress.html';
    })
    $("#userInfo").click(function () {
        location.href = 'userInfo.html';
    })
    $("#myPurchaseTemplate").click(function () {
        location.href = 'myPurchaseTemplate.html';
    })
    $("#myLikeTemplate").click(function () {
        location.href = 'myLikeTemplate.jsp';
    })

    /*阻止事件冒泡*/
    function stopPropagation(e) {
        e = e || window.event;
        if (e.stopPropagation) { //W3C阻止冒泡方法
            e.stopPropagation();
        } else {
            e.cancelBubble = true; //IE阻止冒泡方法
        }
    }
    
    $(".addAddressBg *").click(function(e){
    	stopPropagation(e);
    })

    $(".addAddressBg").click(function(){
    	var inputValue = document.querySelectorAll(".addAddressForm>div>input");
        for(var i=0;i<inputValue.length-1;i++){
            inputValue[i].value = '';
        }
        $(".addAddressBg").hide();
        $('#city-picker-selector').hide();
    })
    $("#addAddress").click(function(){
    	$(".addAddressBg").show();
        $(".alertTitle").text("新增收货地址");
        addOrEdit = 0;
    })

    $(".addAddressAlert,.addAddressAlert>*,.addAddressAlert>form>div,.addAddressAlert>form>div>span,.addAddressAlert>form>div>input").click(function () {
        $('#city-picker-selector').css("display","none");
    });

    $("#addressInput").click(function () {
        $('#city-picker-selector').css("display","block");
    });

    //模拟城市-无联动/无搜索
    var selector = $('#city-picker-selector').cityPicker({
        dataJson: cityData,
        renderMode: true,
        search: false,
        autoSelected: false,
        onForbid: function () {
            console.log(this)
        }
    });

    // 省份选择的回调
    $('#city-picker-selector').on('choose-province.citypicker', function (event, tagert, values) {
        province = values.name;
    });

    // 城市选择的回调
    $('#city-picker-selector').on('choose-city.citypicker', function (event, tagert, values) {
        city = values.name;
    });

    // 城市选择的回调
    $('#city-picker-selector').on('choose-district.citypicker', function (event, tagert, values) {
        if(values.id != 0){
            district = values.name;
            address = province+"/"+city+"/"+district;
            $('#city-picker-selector').css("display","none");
            $("#addressInput").val(address);
        }
    });

    //编辑收货地址时获取收货地址信息
    $(".showChangeAlert").click(function(){
        $(".alertTitle").text("编辑收货地址");
        var id = $(this).parent().parent().attr("id");
        var getAddressSucFunc = function(data){
			$("input[name='personName']").val(data.personName);
			$("input[name='division']").val(data.division);
			$("input[name='address']").val(data.address);
			if(data.phone.length == 12){
				$("input[name='fixphone']").val(data.phone);
			}else{
				$("input[name='phone']").val(data.phone);
			}
			addressId = id; //记录当前准备修改的地址id，方便提交时确定是哪条收货地址
			addOrEdit = 1; //标记现在的form是修改而不是添加收货地址：0代表添加，1代表修改
			$(".addAddressBg").show();
        }
        ajaxByMethod("/address/get", "get", {id: id}, getAddressSucFunc);
    });
    
    //修改默认收货地址
    $(".radioSpan").click(function(){
    	if($(this).hasClass("radioOn")){
    		return;
    	}
    	var id = $(this).parent().attr("id");
    	var changeDefaultAddressSucFunc = function(data){
			$(".radioSpan").removeClass("radioOn");
			$(".defaultAddress").css("display", "none");
			$("#"+id+">span.radioSpan").addClass("radioOn");
			$("#"+id).parent().find(".defaultAddress").css("display", "inline-block");
    	}
    	ajaxByMethod("/address/updateDefaultAddress", "post", {addressId: id}, changeDefaultAddressSucFunc);
    });
    
    //添加收货地址和修改收货地址
    $(".saveAddress>input").click(function(){
    	var inputObject = getValuesByInputName(["personName", "division", "address", "phone", "fixphone"])
    	if(judgeInputValue(inputObject.personName, "notEmpty", "收货人不能为空。")) return;
    	if(judgeInputValue(inputObject.division, "notEmpty", "所在地区不能为空。")) return;
    	if(judgeInputValue(inputObject.address, "notEmpty", "详细地址不能为空。")) return;
    	if(judgeInputValue(inputObject.phone, "orphone")){
    		if(judgeInputValue(inputObject.fixphone, "orfixphone")) return;
    	};
		$(this).attr("disabled",true);
		if(inputObject.phone == ""){
			inputObject.phone = inputObject.fixphone;
		}
		delete inputObject.fixphone;
		inputObject.id = addressId;
		var url = "";
		if(addOrEdit == 0){
			url = "/address/add";
		}else{
			url = "/address/update";
		}
		var saveAddressSucFunc = function(result){
			location.href="/address/list"
		}
		var saveAddressCompleteFunc = function(){
			$(".saveAddress>input").attr("disabled",false);
		}
		ajaxByMethod(url, "post", inputObject, saveAddressSucFunc, null, saveAddressCompleteFunc); 
    });
    
    //删除收货地址
    $(".showDeleteAlert").click(function () {
    	var deleteOkFunction = function(){
        	var deleteSucFunc = function(result){
				$("#"+deleteId).parent().remove();
				$(".addAddressBg").hide();
				showOkAlertTips("删除成功。", function(){location.href="/address/list"});
        	}
        	var deleteCompleteFunc = function(){
        		$(".addAddressBg").hide();
    		}
        	ajaxByMethod("/address/delete", "post", {id: deleteId}, deleteSucFunc, null, deleteCompleteFunc);
        }
    	showOkOrCancelAlertTips("确定删除收货地址？", deleteOkFunction);
        deleteId = $(this).parent().parent().attr("id");
    })
})